<?php
namespace Portal\Controller;
use Common\Controller\HomebaseController;
/**
 * 首页
 */
class RegisterController extends HomebaseController {
	
    //登录
	public function login() {
    	$this->display("Register/login");
    }
    //注册
    public function register() {
        $this->display("Register/register");
    }
    //忘记密码
    public function forget() {
        $this->display("Register/forget");
    }
    //用户注册
    public function doReg() {

        $tel = $_POST["tel"];
        $pass = $_POST["pass"];

        $members = M("members");
        //$arr = $members->select();
        $where["tel"] = $tel;
        $where["pass"] = md5($pass);
        //对密码进行md5加密


        //检查数据库有没有此账户
        $n = $members -> where(array("tel" => $where["tel"])) -> find();//如果单一条件查询时，要写完整条件

        /**
         * 数组查询：
         *  $n = $members -> where($where) -> find();
         * $sql = $members -> getLastSql();
         * 生成sql语句是：
         * SELECT * FROM `cw_members` WHERE `tel` = '18238800887' AND `pass` = 'e10adc3949ba59abbe56e057f20f883e' LIMIT 1
         */

        if($n){
            $this->error("您的账户已经注册过了，请直接登录","Register/login");
        }else{
            /**
             * 修改数据
             * $row = $members->where('id=1')->save($data);
             */

            $row = $members->add($where);
            //$sql = $members->getLastSql();
            if($row){
                $this->success("注册成功,请登录","Register/login");
            }else{
                $this->error("注册失败","Register/register");
            }
        }
    }
    //用户登录
    public function doLogin(){
        $members = M("members");
        $where["tel"] = $_POST['tel'];
        $where["pass"] = md5($_POST['pass']);

        $n = $members -> where($where) -> find();//返回结果是查到的数组
        /**
         * 数组查询：
         * $n = $members -> where($where) -> find();
         * $sql = $members->getLastSql();
         * 生成sql语句：
         * SELECT * FROM `cw_members` WHERE `tel` = '18238800887' AND `pass` = 'e10adc3949ba59abbe56e057f20f883e' LIMIT 1
         *
         */
        if($n){
            //写入登陆者账号

            session('tel',$where["tel"]);
            /**
             * 错误方法
             * session(array('name'=>'session_name','expire'=>3600),'session_value');
             * session(['name'=>'session_name','expire'=>3600]);
             *
             */
            //验证成功返回
            echo "1";
        }else{
            //验证失败返回
            echo "2";
        }

    }
    //退出登录
    public function loginOut(){

        session('tel',null);
        redirect("Index/index");

    }
}


